package com.zky.gulimall.order.listener;

import com.alipay.api.AlipayApiException;
import com.zky.gulimall.order.service.OrderService;
import com.zky.gulimall.order.vo.PayAsyncVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.io.UnsupportedEncodingException;

/**
 * @Description 订单支付成功支付宝回调
 * @Author 张开源
 * @Date 2022/12/23 22:25
 * @Version 1.0
 **/
@RestController
@Slf4j
public class OrderPayedListener {
    @Autowired
    private OrderService orderService;

    @PostMapping("/payed/notify")
    public String orderAliPayedNotify(PayAsyncVo asyncVo, HttpServletRequest request) throws UnsupportedEncodingException, AlipayApiException {
        log.warn("支付宝通知回调所有参数,asyncVo:{}", asyncVo);
        if (asyncVo == null) {
            return null;
        }
        //验签
        Boolean check = orderService.checkSign(request);
        log.warn("orderAliPayedNotify check:{}", check);
        if (!check) {
            return "fail";
        }
        String result = orderService.handlePayResult(asyncVo);
        //只要返回success就不会重试，返回其他都会重试，参考支付宝开放文档
        return result;
    }
}
